ARC090 D - People on a Line
解答
code: python
n, m = map(int, input().split())
class WeightedUnionFind():
def __init__(self, n):
def root(self, x):
return x
root = self.root(self.parentx) self.diff_weightx += self.diff_weight[self.parentx] def weight(self, x):
self.root(x)
def diff(self, x, y):
return self.weight(y) - self.weight(x)
def unite(self, x, y, w):
w += self.weight(x)
w -= self.weight(y)
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.rankx < self.ranky: w = -w
elif self.ranky < self.rankx: else:
def same(self, x, y):
return self.root(x) == self.root(y)
wuf = WeightedUnionFind(n+1)
for l, r, d in lrd:
if wuf.same(l, r):
if wuf.diff(l, r) != d:
print('No')
exit()
else:
wuf.unite(l, r, d)
print('Yes')
テーマ
メモ